﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Smled.States
{
    class SelectState : BaseState
    {
        private bool mouseDown = false;
        private SelectStateToolbar tbar = null;

        public SelectState(MapView mv)
            : base(mv)
        {
            tbar = new SelectStateToolbar();
        }

        public override void MapView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == mv.PrimaryButton)
            {
                mouseDown = true;
                mv.selStart.X = mv.SelectedX;
                mv.selStart.Y = mv.SelectedY;
                mv.selEnd.X = mv.SelectedX;
                mv.selEnd.Y = mv.SelectedY;
                mv.RectSelection = true;
            }
        }

        public override void MapView_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == mv.PrimaryButton)
            {
                mv.selEnd.X = mv.SelectedX;
                mv.selEnd.Y = mv.SelectedY;
                mv.Invalidate();
            }
        }

        public override void MapView_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (mouseDown)
            {
                int oex = mv.SelectionDR.X;
                int oey = mv.SelectionDR.Y;

                mv.selEnd.X = mv.SelectedX;
                mv.selEnd.Y = mv.SelectedY;

                //if ((oex != mv.selEnd.X) && (oey != mv.selEnd.Y)) 
                    mv.Invalidate();
            }
        }

        public override void Activate()
        {
            tbar.RefreshList();
        }

        public override void Deactivate()
        {
            if (mv.selStart.X == mv.selEnd.X && mv.selStart.Y == mv.selEnd.Y)
            {
                mv.rectSelEnabled = false;
                mv.Invalidate();
            }
        }

        public override System.Windows.Forms.ToolStrip ToolBar
        {
            get
            {
                return tbar;
            }
        }
    }
}
